package com.lingjtx.iot.network.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.integration.mqtt.event.MqttConnectionFailedEvent;
import org.springframework.integration.mqtt.event.MqttIntegrationEvent;
import org.springframework.integration.mqtt.event.MqttSubscribedEvent;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Slf4j
@Component
public class MqttConnectionListener implements ApplicationListener<MqttIntegrationEvent> {

    @Override
    public void onApplicationEvent(MqttIntegrationEvent event) {
        if (event instanceof MqttConnectionFailedEvent mqttConnectionFailedEvent) {
            log.error("连接失败: {}", Objects.requireNonNull(mqttConnectionFailedEvent.getCause()).getMessage());
        } else if (event instanceof MqttSubscribedEvent mqttSubscribedEvent) {
            log.info("订阅成功: {}", mqttSubscribedEvent.getMessage());
        }
    }
}
